/**
 * Copyleft (C) 2023-2024 maminjie <canpool@163.com>
 * SPDX-License-Identifier: BSD-3-Clause
 **/
#ifndef QXMATERIALTOGGLE_P_H
#define QXMATERIALTOGGLE_P_H

#include "qxmaterial_global.h"
#include <QColor>
#include <Qt>
#include <QtWidgets/QWidget>
#include "matrippleoverlay.h"

class QStateMachine;
class QState;

QX_MATERIAL_BEGIN_NAMESPACE

class MatToggle;
class MatToggleTrack;
class MatToggleThumb;
class MatToggleRippleOverlay;

class MatTogglePrivate
{
    Q_DISABLE_COPY(MatTogglePrivate)
    Q_DECLARE_PUBLIC(MatToggle)
public:
    MatTogglePrivate(MatToggle *q);
    ~MatTogglePrivate();

    void init();
    void setupProperties();

    MatToggle *const q_ptr;
    MatToggleTrack *track;
    MatToggleThumb *thumb;
    MatToggleRippleOverlay *rippleOverlay;
    QStateMachine *stateMachine;
    QState *offState;
    QState *onState;
    Qt::Orientation orientation;
    QColor disabledColor;
    QColor activeColor;
    QColor inactiveColor;
    QColor trackColor;
    bool useThemeColors;
};

class MatToggleRippleOverlay : public MatRippleOverlay
{
    Q_OBJECT
public:
    MatToggleRippleOverlay(MatToggleThumb *thumb, MatToggleTrack *track, MatToggle *parent);
    ~MatToggleRippleOverlay();

protected slots:
    void addToggleRipple();
protected:
    bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
    QRect overlayGeometry() const Q_DECL_OVERRIDE;
private:
    Q_DISABLE_COPY(MatToggleRippleOverlay)

    MatToggle *const m_toggle;
    MatToggleThumb *const m_thumb;
    MatToggleTrack *const m_track;
};

class MatToggleThumb : public QWidget
{
    Q_OBJECT

    Q_PROPERTY(qreal shift WRITE setShift READ shift)
    Q_PROPERTY(QColor thumbColor WRITE setThumbColor READ thumbColor)
public:
    MatToggleThumb(MatToggle *parent);
    ~MatToggleThumb();

    void setShift(qreal shift);
    inline qreal shift() const;

    inline qreal offset() const;

    inline void setThumbColor(const QColor &color);
    inline QColor thumbColor() const;
protected:
    bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
    void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
    Q_DISABLE_COPY(MatToggleThumb)

    void updateOffset();

    MatToggle *const m_toggle;
    QColor m_thumbColor;
    qreal m_shift;
    qreal m_offset;
};

inline qreal MatToggleThumb::shift() const
{
    return m_shift;
}

inline qreal MatToggleThumb::offset() const
{
    return m_offset;
}

inline void MatToggleThumb::setThumbColor(const QColor &color)
{
    m_thumbColor = color;
    update();
}

inline QColor MatToggleThumb::thumbColor() const
{
    return m_thumbColor;
}

class MatToggleTrack : public QWidget
{
    Q_OBJECT

    Q_PROPERTY(QColor trackColor WRITE setTrackColor READ trackColor)
public:
    MatToggleTrack(MatToggle *parent);
    ~MatToggleTrack();

    void setTrackColor(const QColor &color);
    inline QColor trackColor() const;
protected:
    bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
    void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
    Q_DISABLE_COPY(MatToggleTrack)

    MatToggle *const m_toggle;
    QColor m_trackColor;
};

inline QColor MatToggleTrack::trackColor() const
{
    return m_trackColor;
}

QX_MATERIAL_END_NAMESPACE

#endif   // QXMATERIALTOGGLE_P_H
